﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;

namespace XMLMovieWebsite
{
    public partial class _Default : System.Web.UI.Page
    {   
        protected void Page_Load(object sender, EventArgs e)
        {
            loadDulieuRaFileXML();
            layDulieuTuXML();
            

        }
        public void loadDulieuRaFileXML()
        {
            try
            {
                string ConString = ConfigurationManager.ConnectionStrings["ConStringXml"].ConnectionString;
                //string CMDString = "SELECT id,name,time, rating" + "FROM allMovie FOR XML RAW ('Movie'), ROOT('root'), ELEMENTS";
                string CMDString = " select  TOP 20 * from movie_thao order by NgayDang DESC";
                SqlConnection con;
                SqlCommand cmd;
                SqlDataAdapter sda;
                DataTable dt;


                using (con = new SqlConnection(ConString))
                {
                    cmd = new SqlCommand(CMDString, con);
                    con.Open();
                    dt = new DataTable("Movie");
                    sda = new SqlDataAdapter(cmd);
                    sda.Fill(dt);
                    if (dt != null)
                    {
                                       
                       
                        dt.WriteXml(Server.MapPath("~/App_Data/Hot.xml"));
                    }
                    con.Close();

                }
            }
            catch (Exception)
            {

            }


        }
        public void layDulieuTuXML()
        {

            using (DataSet ds = new DataSet())
            {
                ds.ReadXml(Server.MapPath("~/App_Data/Hot.xml"));
                //GridView1.DataSource = ds;
                //GridView1.DataBind();
                DataTable dt = new DataTable();
                dt = ds.Tables["Movie"];
                //Image1.ImageUrl =  dt.Rows[0][11].ToString();

                Test.DataSource = dt;
                Test.DataBind();

                Paging.PageSize = 4;
                Paging.DataSource = dt.DefaultView;
                Paging.BindToControl = Test;
                Test.DataSource = Paging.DataSourcePaged;
                
            }
        }


    }
}
